import datetime
import os

from web.utils.ningan import DFAFilter
from django.core.exceptions import ValidationError
from django.shortcuts import render, redirect
from web import models
# 正则表达式


from web.models import Scenic, User
from web.utils.pagination import Pagination
from web.views.message import MessageModelForm1


def index(request):
    queryset = Scenic.objects.filter().order_by('-bookcount').all()[0:6]
    scenic1 = models.Scenic.objects.filter(province="福建福州").all()
    scenic2 = models.Scenic.objects.filter(province="福建厦门").all()
    scenic3 = models.Scenic.objects.filter(province="福建泉州").all()
    image_list = models.ImageList.objects.filter(level=1).all()
    return render(request, "index.html", {"queryset": queryset, "scenic1":scenic1, "scenic2":scenic2, "scenic3":scenic3})


os.environ["DJANGO_SETTINGS_MODULE"] = "gwcomments.settings"
curr_dir = os.path.dirname(os.path.abspath(__file__))
wordfilter_path = os.path.join(curr_dir, 'baidu_filter.txt')

def package_details(request, nid):
    """ 添加风景（ModelForm版本）"""
    if request.method == "GET":
        '''原始方法'''
        queryset = models.Scenic.objects.filter(id=nid).first()
        type_choices = models.Bookinfo.type_choices
        # 用户POST提交数据，数据校验。
        form = MessageModelForm1(data=request.POST, files=request.FILES)
        # return render(request, 'package-details.html', {"obj": queryset, "type_choices":type_choices,'form',form})
        # return render(request, 'package-details.html', {"obj": queryset, "type_choices":type_choices,"form": form})
        return render(request, 'package-details.html', {"obj": queryset, "type_choices":type_choices})
    # 添加预约申请
    bookname = request.POST.get("bookname")
    email = request.POST.get("email")
    phone = request.POST.get("phone")
    booktime = request.POST.get("booktime")
    type = request.POST.get("type")
    description = request.POST.get("description")
    # 外键要先实例化再提交
    uid = User.objects.get(id=int(request.POST.get("uid")))
    subtime = datetime.datetime.now().strftime("%Y-%m-%d")
    gfw = DFAFilter()
    path = wordfilter_path
    gfw.parse(path)
    text = description
    result = gfw.filter(text)
    models.Bookinfo.objects.create(bookname=bookname, email=email, phone=phone, booktime=booktime, type=type, description=result,
                                   sceid_id=nid, uid=uid, subtime=subtime)
    # 更新预约次数数据
    row_obj = Scenic.objects.filter(id=nid).first()
    bookcount = row_obj.bookcount+1
    Scenic.objects.filter(id=nid).update(bookcount=bookcount)
    return redirect("/mybookInfo/")




def package(request):
    queryset = Scenic.objects.all()

    return render(request, "package.html", {"queryset": queryset})

def about(request):
    return render(request, 'about.html')

def contact(request):
    if request.method == "GET":
        '''原始方法'''
        return render(request, 'contact.html')
    messagename = request.POST.get("messagename")
    subject = request.POST.get("subject")
    email = request.POST.get("email")
    phone = request.POST.get("phone")
    context = request.POST.get("context")
    userid = request.POST.get("user_id")
    print(userid)
    models.Message.objects.create(messagename=messagename, subject=subject, email=email, phone=phone,
                                  context=context, reply=1, user_id=userid)
    return redirect("/index/")

def mybookInfo(request):
    info_dict = request.session["info"]
    uid = info_dict["id"]
    queryset = models.Bookinfo.objects.filter(uid=uid).all()

    page_object = Pagination(request, queryset)

    context = {
        "queryset": page_object.page_queryset,  # 分完页的数据
        "page_string": page_object.html()  # 页码
    }
    return render(request, "mybookInfo.html", context)
